/**
 * 
 */
package org.wltea.analyzer.dic;

import org.wltea.analyzer.Lexeme;

/**
 * IK Analyzer v3.2 表示词典检索的命中结果
 * 
 * @author 林良益
 * 
 */
public class Hit {
	// Hit不匹配
	private static final int UNMATCH = 0x00000000;
	// Hit完全匹配
	private static final int MATCH = 0x00000001;
	// Hit前缀匹配
	private static final int PREFIX = 0x00000010;

	// 该HIT当前状态，默认未匹配
	private int hitState = UNMATCH;

	// 记录词典匹配过程中，当前匹配到的词典分支节点
	private DictSegment matchedDictSegment;
	/*
	 * 词段开始位置
	 */
	private int begin;
	/*
	 * 词段的结束位置
	 */
	private int end;

	private int lexemeType = -1;

	private boolean isExt;

	public void addLexemeType(int type) {
		if (lexemeType == -1) {
			if (isMatch() && type == Lexeme.TYPE_CJK_UNKNOWN) {
				lexemeType = Lexeme.TYPE_CJK_NORMAL;
			} else {
				lexemeType = type;
			}
		} else if (type == Lexeme.TYPE_CJK_UNKNOWN) {
			if (lexemeType == Lexeme.TYPE_CJK_UNKNOWN) {
				lexemeType = Lexeme.TYPE_CJK_NORMAL;
			} else if (lexemeType != Lexeme.TYPE_CJK_NORMAL) {
				lexemeType = Lexeme.TYPE_CJK_MIX;
			}
		} else {
			if (lexemeType == Lexeme.TYPE_CJK_NORMAL
					|| lexemeType == Lexeme.TYPE_CJK_UNKNOWN
					|| lexemeType == Lexeme.TYPE_CJK_MIX) {
				lexemeType = Lexeme.TYPE_CJK_MIX;
			} else {
				lexemeType = Lexeme.TYPE_PHRASE;
			}
		}
	}

	public int getLexemeType() {
		return lexemeType;
	}

	/**
	 * 判断是否完全匹配
	 */
	public boolean isMatch() {
		return (this.hitState & MATCH) > 0;
	}

	/**
	 * 
	 */
	public void setMatch(boolean isExt) {
		this.hitState = this.hitState | MATCH;
		this.isExt = isExt;
	}

	/**
	 * 判断是否是词的前缀
	 */
	public boolean isPrefix() {
		return (this.hitState & PREFIX) > 0;
	}

	/**
	 * 
	 */
	public void setPrefix() {
		this.hitState = this.hitState | PREFIX;
	}

	/**
	 * 判断是否是不匹配
	 */
	public boolean isUnmatch() {
		return this.hitState == UNMATCH;
	}

	/**
	 * 
	 */
	public void setUnmatch() {
		this.hitState = UNMATCH;
	}

	public DictSegment getMatchedDictSegment() {
		return matchedDictSegment;
	}

	public void setMatchedDictSegment(DictSegment matchedDictSegment) {
		this.matchedDictSegment = matchedDictSegment;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public boolean isExt() {
		return isExt;
	}

	@Override
	public String toString() {
		return "Hit [hitState=" + hitState + ", begin=" + begin + ", end="
				+ end + ", isExt=" + isExt + "]";
	}

}
